﻿using Common.Library.Files;
using CreateData.Entitys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Common.Library.Db;

namespace CreateData
{
    public class CreateLocationData
    {
        public static void CreateData()
        {
            string json = FileStreamhelper.ReadFile("D://1.txt");
            List<ProvinceEntity> list = JsonConvert.DeserializeObject<List<ProvinceEntity>>(json);

            #region 为所有的 省市县街道片区添加 主键ID等信息
            int locationId = 1;
            foreach (var p in list)
            {
                p.LocationId = locationId;
                locationId++;
            }
            foreach (var p in list)
            {
                foreach (var c in p.CityList)
                {
                    c.ParentId = p.LocationId;
                    c.LocationId = locationId;
                    c.CityId = locationId;
                    c.ProvinceId = p.LocationId;
                    c.Level = 2;
                    locationId++;
                    foreach (var county in c.CountyList)
                    {
                        county.ParentId = c.LocationId;
                        county.LocationId = locationId;
                        county.CountyId = locationId;
                        county.ProvinceId = p.LocationId;
                        county.CityId = c.LocationId;
                        county.Level = 3;
                        locationId++;
                        foreach (var s in county.StreetList)
                        {
                            s.ParentId = county.LocationId;
                            s.LocationId = locationId;
                            s.StreetId = locationId;
                            s.ProvinceId = p.LocationId;
                            s.CityId = c.LocationId;
                            s.CountyId = county.LocationId;
                            s.Level = 4;
                            locationId++;
                            foreach (var a in s.AreaList)
                            {
                                a.ParentId = s.LocationId;
                                a.LocationId = locationId;
                                a.AreaId = locationId;
                                a.ProvinceId = p.LocationId;
                                a.CityId = c.LocationId;
                                a.CountyId = county.LocationId;
                                a.StreetId = s.LocationId;
                                a.Level = 5;
                                locationId++;
                            }
                        }
                    }
                }
            }
            #endregion

            #region 数据录入数据库中
            List<LocationEntity> provinceEntity = new List<LocationEntity>();

            #region 构建列表
            foreach (var p in list)
            {
                foreach (var c in p.CityList)
                {
                    provinceEntity.Add(new LocationEntity()
                    {
                        LocationId = c.LocationId,
                        LocationName = c.CityName,
                        AreaId = c.AreaId,
                        CityId = c.CityId,
                        CountyId = c.CountyId,
                        GB11643 = 0,
                        GB11643_1999 = c.GB11643_1999,
                        Level = c.Level,
                        ParentId = c.ParentId,
                        ProvinceId = c.ProvinceId,
                        StreetId = c.StreetId
                    });
                    foreach (var county in c.CountyList)
                    {
                        provinceEntity.Add(new LocationEntity()
                        {
                            LocationId = county.LocationId,
                            LocationName = county.CountyName,
                            AreaId = county.AreaId,
                            CityId = county.CityId,
                            CountyId = county.CountyId,
                            GB11643 = 0,
                            GB11643_1999 = county.GB11643_1999,
                            Level = county.Level,
                            ParentId = county.ParentId,
                            ProvinceId = county.ProvinceId,
                            StreetId = county.StreetId
                        });
                        foreach (var s in county.StreetList)
                        {
                            provinceEntity.Add(new LocationEntity()
                            {
                                LocationId = s.LocationId,
                                LocationName = s.StreetName,
                                AreaId = s.AreaId,
                                CityId = s.CityId,
                                CountyId = s.CountyId,
                                GB11643 = 0,
                                GB11643_1999 = s.GB11643_1999,
                                Level = s.Level,
                                ParentId = s.ParentId,
                                ProvinceId = s.ProvinceId,
                                StreetId = s.StreetId
                            });
                            foreach (var a in s.AreaList)
                            {
                                provinceEntity.Add(new LocationEntity()
                                {
                                    LocationId = a.LocationId,
                                    LocationName = a.AreaName,
                                    AreaId = a.AreaId,
                                    CityId = a.CityId,
                                    CountyId = a.CountyId,
                                    GB11643 = 0,
                                    GB11643_1999 = a.GB11643_1999,
                                    Level = a.Level,
                                    ParentId = a.ParentId,
                                    ProvinceId = a.ProvinceId,
                                    StreetId = a.StreetId
                                });
                            }
                        }
                    }
                }
            }
            DbHelp.InsertLocation(provinceEntity);
            #endregion

            #region 插数据
            List<LocationEntity> tempList = new List<LocationEntity>();
            int j = 0;
            for (int i = 0; i < provinceEntity.Count; i++)
            {
                j++;
                tempList.Add(provinceEntity[i]);
                if (j == 100000)
                {

                    DbHelp.InsertLocation(tempList);
                    tempList = new List<LocationEntity>();
                    j = 0;
                }
            }
            DbHelp.InsertLocation(tempList);
            #endregion

            #endregion

            #region 垃圾
            /////首先把省级信息插入到数据库中
            /////  在遍历省级列表
            /////  查找到北京的Id
            /////     把市级的信息插入到数据库中
            /////     遍历市级列表
            /////     查找到该市的Id
            /////         把区县的数据插入到数据库中
            /////         遍历区县
            /////         查找到区县的Id
            /////             把街道的数据插入到数据库中
            /////             遍历街道
            /////             查找到街道的Id
            /////                 把片区的数据插入到数据库中
            /////                 
            //List<LocationEntity> provinceEntity = new List<LocationEntity>();
            //int locationId = 1;
            //foreach (var item in listEntity)
            //{
            //    provinceEntity.Add(new LocationEntity()
            //    {
            //        AreaId = 0,
            //        CityId = 0,
            //        CountyId = 0,
            //        GB11643 = 0,
            //        GB11643_1999 = "",
            //        Level = 1,
            //        LocationName = item.ProvinceName,
            //        StreetId = 0,
            //        ParentId = 0,
            //        ProvinceId = 0,
            //        LocationId = locationId
            //    });
            //    locationId++;
            //}
            //IAppDBContext appDbContext = AppDBContextFactory.GetAppDBContext();
            //foreach (var item in listEntity)
            //{
            //    //省信息
            //    //LocationEntity entity = appDbContext.Get<LocationEntity>($"select * from dbo.locationAll where locationName = '{item.ProvinceName}'").FirstOrDefault();
            //    List<CityEntity> cityList = item.CityList;
            //    List<LocationEntity> list = new List<LocationEntity>();
            //    foreach (var city in cityList)
            //    {
            //        //list.Add(new LocationEntity
            //        //{
            //        //    ParentId = entity.LocationId,
            //        //    CityId = 0,
            //        //    Level = 2,
            //        //    AreaId = 0,
            //        //    ProvinceId = entity.LocationId,
            //        //    GB11643_1999 = city.GB11643_1999,
            //        //    StreetId = 0,
            //        //    CountyId = 0,
            //        //    GB11643 = 0,
            //        //    LocationName = city.CityName
            //        //});
            //    }

            //} 
            #endregion




        }
    }
}
